<template>
  <div class="policy-release common-style">
  	<div class="policy-release-header">
  		<div class="fl">
  			<span>政策类型</span>
				<multi :optionsdata="selectPublish" :selecteddata="selectedPublish" @item="obtainContent" :dataindex="2" :displayname="'value'" :initialization="'请选择政策类型'"></multi>
				<span>性质分类</span>
				<multi :optionsdata="selectNature" :selecteddata="selectedNature" @item="obtainNature" :dataindex="4" :displayname="'value'" :initialization="'请选择性质分类'"></multi>
				<span>地区</span>
				<select class="select" v-model="area" >
					<option value="">请选择地区</option>
					<option v-for="item in regionList" :value="item.id">{{item.value}}</option>
				</select>
  			<input class="input" v-model="searchContent">
  			<button class="Public-btn" @click="searchList()">搜索</button>
  			
  		</div>
  		<button class="Public-btn fr" @click="policy('add')">新增</button>
  	</div>
  	<div>
  		<span class="eg">政策列表内部链接示例：comprehensivePolicy/comprehensivePolicy?type=11</span>
  		<span class="eg">政策详情内部链接示例：articleDetail/articleDetail?judgeType=unscramble&id=xxx</span>
  	</div>
  	<div class="table-father">
	    <table class="table">
				<thead>
					<th>序号</th>
					<th>标题</th>
					<th>文件号</th>
					<th>文件时间</th>
					<th>发布人</th>
					<th>发布时间</th>
					<th>浏览量</th>
					<th>id值</th>
					<th>状态</th>
					<th>操作</th>
				</thead>
				<tbody>
					<tr v-for="(item,index) in tableList">
						<td><span>{{index+1}}</span></td>
						<td class="cursor" :title="item.title">
							<span v-if="item.title.length>15">{{item.title.substring(0,15)}}....</span>
							<span v-if="item.title.length<16">{{item.title}}</span>
						</td>
						<td class="cursor" :title="item.fileNumber">
							<span v-if="item.fileNumber.length>15">{{item.fileNumber.substring(0,15)}}....</span>
							<span v-if="item.fileNumber.length<16">{{item.fileNumber}}</span>
						</td>
						<td><span>{{item.fileDate}}</span></td>
						<td><span>{{item.publishBy}}</span></td>
						<td><span>{{item.publishAt}}</span></td>
						<td><span>{{item.pageView}}</span></td>
						<td><span>{{item.id}}</span></td>
						<td>
							<span v-if="item.status==1">待发布</span>
							<span v-if="item.status==2">已发布</span>
						</td>
						<td>
							<button class="Public-btn" @click="policy('lookOver',item.id)">查看</button>
							<button class="Public-btn" v-if="item.status==1"  @click="policy('modify',item.id)">修改</button>
							<button class="Public-btn" v-if="item.status==1" @click="publishData(item.id,'2',index)">发布</button>
							<button class="Public-btn" v-if="item.status==1"  @click="policy('modify',item.id)">添加解读</button>
							<button class="Public-btn" v-if="item.status==2" @click="publishData(item.id,'1',index)">撤回</button>
							<button class="Public-btn" v-if="item.status==1" @click="delArticle(item.id)">删除</button>
						</td>
					</tr>
					<tr>
						<td colspan="10" style="color: red" v-if="!tableList.length">暂无数据</td>
					</tr>
				</tbody>
			</table>
		</div>
  	<Pageination  :total="model.total" :size="model.size" :page="model.page" :changge="pageFn"></Pageination>
  </div>
</template>
<script>
	import Pageination from "../shareComponent/pageination";
	import multi from "../shareComponent/multiSelect";
	export default {
    name:'policy-release',
    data:function(){
	    return{
	    	/**分页初始化**/
		    model: {
	        total: 0,//总数
	        size: 10,//每页显示条目个数不传默认20
	        page: 1,//当前页码
		    },
	      tableList:[],
	      /*检索内容*/
	     	searchContent:"",
	    	/*提示名称*/
	    	title:"",
	    	/*内容选中个数*/
	    	content:0,
	    	/*性质选中个数*/
	    	nature:0,
	    	/*当前页码*/
	    	currentPage:1,
	    	 /*下拉内容多选数据*/
       	selectPublish:[],
        selectedPublish:[],
        /*下拉性质多选数据*/
       	selectNature:[],
        selectedNature:[],
        /*地区*/
        regionList:[],
        /* 选择地区 */
				area:"",
				/* 父级id 2政策3性质 */
				parentCategory:"",
				/* 底层类别 */
				childCategory:"",
				/*内容*/
      	contentChildCategory:[],
      	/*性质*/
      	natureChildCategory:[],
      }
    },
    created: function(){
    	$(window).scrollTop(0);
    	
    },
    components:{Pageination,multi},
    mounted:function() {
    	this.queryContent();
    	$(".show-select-name").css({"width":"180px","height":"36px","border": "1px solid #ccc","cursor":"pointer"});
    	$(".all-option").css("width","180px");
    },
    methods:{
    	/*搜索*/
    	searchList:function(){
    		this.model.page=1;
    		this.currentPage=1;
    		this.queryQuestionByType();
    	},
    	/*获取内容下拉框值*/
    	/*获取性质下拉框值*/
    	queryContent:function(){
    		this.$get('/system/queryDataByParentId/1')
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
	            this.regionList=response.resultData;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
    		this.$get('/system/queryDataByParentId/2')
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
	            this.selectPublish=response.resultData;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
        this.$get('/system/queryDataByParentId/3')
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
	            this.selectNature=response.resultData;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
    	},
    	/*获取列表数据*/
    	queryQuestionByType:function(){
    		this.content=this.contentChildCategory.length;
      	this.nature=this.natureChildCategory.length;
      	this.childCategory="";
      	this.parentCategory="";
    		if(this.content||this.nature){
    			if(this.content>0&&this.nature==0){
    				this.parentCategory="2";
    			}
      		if(this.content==0&&this.nature>0){
    				this.parentCategory="3";
    			}if(this.content>0&&this.nature>0){
    				this.parentCategory="2,3";
    			}
	      	for(var i=0;i<this.content;i++){
	      		this.childCategory+=this.contentChildCategory[i].id+",";
	      	}
	      	for(var i=0;i<this.nature;i++){
	      		this.childCategory+=this.natureChildCategory[i].id+",";
	      	}
	      	this.childCategory=this.childCategory.substring(0,this.childCategory.length-1);
    		}
    		this.$post(
          '/article/queryArticle',
          {
	          "type":"11",
	          "page":this.model.page,
	          "size":"10",
	          "orderBy":"file_date",
	          "searchContent":this.searchContent,
	          "area":this.area,
	          "parentCategory":this.parentCategory,
	          "childCategory":this.childCategory
          }
        )
    		.then(response => {
    			/*	未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		if(!response.resultData.articleList){
          			this.tableList =[]
          		}else{
          			this.tableList = response.resultData.articleList;
		            for(var i=0;i<this.tableList.length;i++){
		            	this.tableList[i].publishAt=this.tableList[i].publishAt?this.formatTime(this.tableList[i].publishAt):"";
		            	this.tableList[i].fileDate=this.tableList[i].fileDate?this.formatTime(this.tableList[i].fileDate):"";
		            }
          		}
	            this.model.total = response.resultData.totalCount;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
        }).catch(err => {
          console.log(err);
        })
    	},
    	/*页码切换执行方法*/
			pageFn:function(val){
				this.model.page = parseInt(val);
				this.currentPage=parseInt(val);
				//此处调列表数据接口
				this.queryQuestionByType();
			},
			/*发布/修改*/
			policy:function(type,id){
				this.$router.push({path:"/main/createPublishing?currentType="+type+"&id="+id});
			},
			 /*获取内容*/
      obtainContent:function(data){
      	this.contentChildCategory=data;
      },
       /*获取性质*/
      obtainNature:function(data){
      	this.natureChildCategory=data;
      },
			/*撤回/发布*/
			publishData:function(id,status,index){
				var _this=this;
				if(status==1){
					this.title="撤回";
					this.content="您确定要撤回这条数据吗？";
				}else if(status==2){
					this.title="发布";
					this.content="您确定要发布这条数据吗？";
				}
				var indexs=layer.confirm(_this.content, {
					title:_this.title,
					btnAlign:"c",
				  btn: ['确定','取消'] //按钮
				}, function(){
					layer.close(indexs);
					_this.$get('/article/updatestatus/'+id+"/"+status)
	    		.then(response => {
	    			/*未登录重定向*/
	          if(response==444){
	          	_this.$router.push({path: '/login'});
	          }else{
	          	if (response.resultCode ==0) {
	          		if(status==1){
									layer.alert("撤回成功");
								}else if(status==2){
									layer.alert("发布成功");
								}
	          		_this.queryQuestionByType();
		          }else{
		          	layer.alert(response.resultMsg);
		          }
	          }
	         
	        }).catch(err => {
	          console.log(err);
	        });
				});
			},
			/*删除*/
			delArticle:function(id){
				var _this=this;
				layer.confirm('您确定要删除这条数据吗？', {
					title:"删除",
					btnAlign:"c",
				  btn: ['确定','取消'] //按钮
				}, function(){
					_this.$get('/article/delArticle/'+id)
	    		.then(response => {
	    			/*未登录重定向*/
	          if(response==444){
	          	_this.$router.push({path: '/login'});
	          }else{
	          	if (response.resultCode ==0) {
	          		layer.alert("删除成功");
	          		var num = Math.ceil(_this.model.total / _this.model.size)-1;
	          		if(_this.model.total-(_this.model.size*num)>1||_this.tableList.length>1){
	          			_this.model.page=_this.currentPage;
	          		}else if(_this.model.total==1){
	          			_this.model.page==1
	          		}else{
	          			_this.model.page=parseInt(_this.currentPage)-1;
	          		}
			    			_this.queryQuestionByType();
		          }else{
		          	layer.alert(response.resultMsg);
		          }
	          }
	         
	        }).catch(err => {
	          console.log(err);
	        });
				});
			}
		}
  }
</script>
<style  lang="less" scoped>
	.common-style > div:first-child{
		margin-bottom: 10px;
		line-height: 18px;
	}
	div.multi{
		width: 180px;
    display: inline-block;
    top: 14px;
    margin-right: 10px;
	}
</style>
